package com.fd.cloud.fdftp.config;

import com.fd.cloud.fdftp.Pool.FtpClientPool;
import com.fd.cloud.fdftp.factory.FtpClientFactory;
import com.fd.cloud.fdftp.model.FtpConfigOptions;
import com.fd.cloud.fdftp.util.FtpUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 配置类
 */
@Configuration
@EnableConfigurationProperties(FtpConfigOptions.class)
public class FtpConfiguration {

    @Autowired
    FtpConfigOptions ftpConfigOptions;

    /**
     * 创建FtpClientFactory的bean对象
     * @return
     */
    @Bean
    public  FtpClientFactory ftpClientFactory(){
        FtpClientFactory factory= new FtpClientFactory(ftpConfigOptions);
        return factory;
    }
    /**
     * FtpClientPool
     * @return
     */
    @Bean
    public FtpClientPool ftpClientPool(){
        ftpConfigOptions.setJmxEnabled(false);
        FtpClientPool ftpClientPool =new FtpClientPool(ftpClientFactory(),ftpConfigOptions);
        try {
            //显示的调用生成连接池
            ftpClientPool.preparePool();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ftpClientPool;
    }
    /**
     * FtpUtils
     * @return
     */
    @Bean
    public FtpUtils ftpUtils(){
        FtpUtils ftpUtils= new FtpUtils(ftpClientPool());
        return ftpUtils;
    }
}
